package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.exception.YYGHException;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/*==========================================
      时 间:   2022-04-19
      讲 师:   刘  辉
      描 述:   尚硅谷教学团队
 ==========================================*/
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    //保存医院信息
    @Override
    public void save(Map<String, Object> stringObjectMap) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(stringObjectMap), Department.class);
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();

        Department mongoDepartment=departmentRepository.findByHoscodeAndDepcode(hoscode,depcode);

        if(mongoDepartment == null){
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else{
            department.setCreateTime(mongoDepartment.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(mongoDepartment.getIsDeleted());
            department.setId(mongoDepartment.getId());
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> getDepartmentPage(Map<String, Object> stringObjectMap) {
        String hoscode = (String)stringObjectMap.get("hoscode");
        String page = (String)stringObjectMap.get("page");
        String limit = (String)stringObjectMap.get("limit");

        Department department=new Department();
        department.setHoscode(hoscode);
        department.setIsDeleted(0);
        Example<Department> departmentExample=Example.of(department);

        PageRequest pageRequest = PageRequest.of(Integer.parseInt(page) - 1, Integer.valueOf(limit), Sort.by(Sort.Direction.ASC, "createTime"));
        return departmentRepository.findAll(departmentExample, pageRequest);
    }

    @Override
    public void remove(Map<String, Object> stringObjectMap) {
        String hoscode = (String)stringObjectMap.get("hoscode");
        String depcode = (String)stringObjectMap.get("depcode");
        Department byHoscodeAndDepcode = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if(byHoscodeAndDepcode == null){
           throw new YYGHException(20001,"该科室不存在");
        }

        byHoscodeAndDepcode.setIsDeleted(1);
        departmentRepository.save(byHoscodeAndDepcode);
      //  departmentRepository.deleteById(byHoscodeAndDepcode.getId());
    }

    //
    @Override
    public List<DepartmentVo> getDepartmentList(String hoscode) {
        Department department=new Department();
        department.setHoscode(hoscode);
        Example<Department> example=Example.of(department);
        List<Department> all = departmentRepository.findAll(example);

        List<DepartmentVo> bigDepartmentList=new ArrayList<DepartmentVo>();

        //key:bigcode,小科室信息
        Map<String, List<Department>> collect = all.stream().collect(Collectors.groupingBy(Department::getBigcode));

        //遍历Map,拿到大科室编号及大科室底下所有的子科室信息
        for (Map.Entry<String, List<Department>> stringListEntry : collect.entrySet()) {
            String key = stringListEntry.getKey();
            List<Department> value = stringListEntry.getValue();

            //1.准备每个大科室数据
            DepartmentVo bigDepartmentVo=new DepartmentVo();
            bigDepartmentVo.setDepcode(key);
            bigDepartmentVo.setDepname(value.get(0).getBigname());

            List<DepartmentVo> smallDepartmentVoList=new ArrayList<DepartmentVo>();

            for (Department department1 : value) {
                String depcode = department1.getDepcode();
                String depname = department1.getDepname();

                DepartmentVo smallDepartmentVo=new DepartmentVo();
                smallDepartmentVo.setDepcode(depcode);
                smallDepartmentVo.setDepname(depname);
                //把每个小科室添加到小科室列表中
                smallDepartmentVoList.add(smallDepartmentVo);
            }

            bigDepartmentVo.setChildren(smallDepartmentVoList);

            //2.添加每个大科室数据到大科室列表中
            bigDepartmentList.add(bigDepartmentVo);
        }

        return bigDepartmentList;
    }

    @Override
    public Department findByHoscodeAndDepcode(String hoscode, String depcode) {
        return  departmentRepository.findByHoscodeAndDepcode(hoscode,depcode);
    }
}
